package cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord;

import cn.iocoder.yudao.module.handguard.controller.app.processrecord.vo.ProcessRecordRespVO;
import cn.iocoder.yudao.module.handguard.convert.processrecord.ProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.processrecord.ProcessRecordDO;
import cn.iocoder.yudao.module.handguard.service.order.OrderService;
import cn.iocoder.yudao.module.handguard.service.processrecord.ProcessRecordService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.handguard.controller.app.orderprocessrecord.vo.*;
import cn.iocoder.yudao.module.handguard.dal.dataobject.orderprocessrecord.OrderProcessRecordDO;
import cn.iocoder.yudao.module.handguard.convert.orderprocessrecord.OrderProcessRecordConvert;
import cn.iocoder.yudao.module.handguard.service.orderprocessrecord.OrderProcessRecordService;

@Tag(name = "小程序 - 订单审核流程记录")
@RestController
@RequestMapping("/hutai/order-process-record")
@Validated
public class OrderProcessRecordController {

    @Resource
    private OrderProcessRecordService orderProcessRecordService;

    @Resource
    private ProcessRecordService processRecordService;

    @Resource
    private OrderService orderService;

    @PostMapping("/create")
    @Operation(summary = "保存受理审核流程记录")
    public CommonResult<String> createOrderProcessRecord(@Valid @RequestBody OrderProcessRecordCreateReqVO createReqVO) {
        orderProcessRecordService.createOrderProcessRecord(createReqVO);
        return CommonResult.success("操作成功");
    }

    @PostMapping("/finish")
    @Operation(summary = "受理审核流程结单")
    public CommonResult<String> finish(@Valid @RequestBody OrderProcessRecordFinishReqVO finishReqVO) {
        orderService.updateOrderStateAndAmount(finishReqVO.getOrderId(), finishReqVO.getRealAmount());
        return CommonResult.success("操作成功");
    }

    @GetMapping("/list/by/order")
    @Operation(summary = "根据订单ID查询审核流程记录")
    @Parameter(name = "orderId", description = "订单编号", required = true, example = "1024")
    public CommonResult<List<ProcessRecordRespVO>> getOrderProcessRecordList(@RequestParam("orderId") Long orderId) {
        List<ProcessRecordDO> list = processRecordService.getProcessRecordList(orderId);
        return success(ProcessRecordConvert.INSTANCE.convertList(list));
    }

//    @PutMapping("/update")
//    @Operation(summary = "更新订单审核流程记录")
//    @PreAuthorize("@ss.hasPermission('hutai:order-process-record:update')")
//    public CommonResult<Boolean> updateOrderProcessRecord(@Valid @RequestBody OrderProcessRecordUpdateReqVO updateReqVO) {
//        orderProcessRecordService.updateOrderProcessRecord(updateReqVO);
//        return success(true);
//    }

//    @DeleteMapping("/delete")
//    @Operation(summary = "删除订单审核流程记录")
//    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('hutai:order-process-record:delete')")
//    public CommonResult<Boolean> deleteOrderProcessRecord(@RequestParam("id") Long id) {
//        orderProcessRecordService.deleteOrderProcessRecord(id);
//        return success(true);
//    }

//    @GetMapping("/get")
//    @Operation(summary = "获得订单审核流程记录")
//    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('hutai:order-process-record:query')")
//    public CommonResult<OrderProcessRecordRespVO> getOrderProcessRecord(@RequestParam("id") Long id) {
//        OrderProcessRecordDO orderProcessRecord = orderProcessRecordService.getOrderProcessRecord(id);
//        return success(OrderProcessRecordConvert.INSTANCE.convert(orderProcessRecord));
//    }



//    @GetMapping("/page")
//    @Operation(summary = "获得订单审核流程记录分页")
//    public CommonResult<PageResult<OrderProcessRecordRespVO>> getOrderProcessRecordPage(@Valid OrderProcessRecordPageReqVO pageVO) {
//        PageResult<OrderProcessRecordDO> pageResult = orderProcessRecordService.getOrderProcessRecordPage(pageVO);
//        return success(OrderProcessRecordConvert.INSTANCE.convertPage(pageResult));
//    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出订单审核流程记录 Excel")
//    @PreAuthorize("@ss.hasPermission('hutai:order-process-record:export')")
//    @OperateLog(type = EXPORT)
//    public void exportOrderProcessRecordExcel(@Valid OrderProcessRecordExportReqVO exportReqVO,
//              HttpServletResponse response) throws IOException {
//        List<OrderProcessRecordDO> list = orderProcessRecordService.getOrderProcessRecordList(exportReqVO);
//        // 导出 Excel
//        List<OrderProcessRecordExcelVO> datas = OrderProcessRecordConvert.INSTANCE.convertList02(list);
//        ExcelUtils.write(response, "订单审核流程记录.xls", "数据", OrderProcessRecordExcelVO.class, datas);
//    }

}
